/** 工单
*/
import request from '@/utils/request'
import { filtersRaw, pageRequsetBody } from '@/utils'

/**
 * 保存
 *@params {object} data
 */
export function postWorkOrderRecord(data) {
  return request({
    url: '/call-center/api/v2/workOrderRecord',
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data
  })
}
/**
 * 人员变更
 *@params {array} data
 */
export function putWorkOrderHandler(workOrderId, data) {
  return request({
    url: `/call-center/api/v1/workOrderHandler/${workOrderId}/changeHandlers`,
    method: 'PUT',
    data
  })
}
/** 工单导出 */
export function workOrderExport(data) {
  return request({
    url: '/call-center/api/v1/workOrderRecord/export',
    method: 'POST',
    data,
    responseType: 'blob',
    timeout: 10 * 60 * 1000
  })
}
/** 修改
 *@params {object} data
 */
export function putWorkOrderRecord(data) {
  return request({
    url: '/call-center/api/v2/workOrderRecord',
    method: 'PUT',
    data
  })
}
/** 查询所有
 */
export function getWorkOrderRecord() {
  return request({
    url: '/call-center/api/v1/workOrderRecord',
    method: 'GET'
  })
}
/** 根据id查询
 * @params {number} id 工单id
 */
export function getByIdWorkOrderRecord(id) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/${id}`,
    method: 'GET'
  })
}
/** 删除
 * @params {number} id 工单id
 */
export function deleteWorkOrderRecord(id) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/${id}`,
    method: 'DELETE'
  })
}
/** 抄送
 * @params {object} data
 */
export function postCarbonCopy(workOrderId, data) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/carbonCopy/${workOrderId}`,
    method: 'POST',
    data
  })
}

/**
 * 改派
 * @param {String} workOrderId  工单id
 * @param {Array} data 改派数据
 */
export function putWorkOrderReassign(workOrderId, data) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/reassign/${workOrderId}`,
    method: 'PUT',
    data
  })
}

/** 批量删除
 * @params {array} ids
 */
export function deleteBatch(ids) {
  return request({
    method: 'DELETE',
    url: '/call-center/ap2/v1/workOrderRecord/deleteBatch',
    data: {
      ids
    }
  })
}
/** 派工单
 * @params {object} data
 */
export function postDispatch(data) {
  return request({
    method: 'POST',
    url: '/call-center/api/v2/workOrderRecord/dispatch',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: data
  })
}
/** 完结工单
 * @parmas {object} data
 */
export function putFinish(data) {
  return request({
    method: 'PUT',
    url: '/call-center/api/v2/workOrderRecord/finish',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data
  })
}
/** 提交工单处理
 * @parmas {object} data
 */
export function putSubmit(data) {
  return request({
    method: 'PUT',
    url: '/call-center/api/v2/workOrderRecord/submit',
    data
  })
}
/** 工单查询-提交工单处理
 * @parmas {object} data
 */
export function putQueryOrderSubmit(workOrderId, data) {
  return request({
    method: 'PUT',
    url: `/call-center/api/v2/workOrderRecord/query/${workOrderId}/submit`,
    data
  })
}

/** 同步工单
 * @parmas {object} data
 */
export function postSyncWorkOrder(data) {
  return request({
    method: 'POST',
    url: '/call-center/api/v1/workOrderRecord/syncWorkOrder',
    data
  })
}

/**
 * 工单催办
 * @param {String} id  工单id
 * @param {Array} urgeIdList  接收人表ID集合
 */
export function putUrgeWorkOrder({ id, urgeIdList }) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/urge/${id}`,
    method: 'PUT',
    data: urgeIdList
  })
}

// 非cc

/** 非cc提交留言处理信息
 * @params {object} data
 */
export function putOpenWorkOrderRecord(data) {
  return request({
    method: 'POST',
    url: '/call-center/openapi/v2/workOrderRecord/uip/commit',
    data
  })
}
/** 查询所有
 */
export function getOpenWorkOrderRecord() {
  return request({
    method: 'GET',
    url: '/call-center/openapi/v1/workOrderRecord'
  })
}
/** 根据id查询
 * @params {string} id
 */
export function getByIdOpenWorkOrderRecord(id) {
  return request({
    method: 'GET',
    url: `/call-center/openapi/v2/workOrderRecord/uip/${id}`
  })
}
/** 接收工单
 * @param {Object} data:{deptName,handler,handlerName,id}
 */
export function postAccept(data) {
  return request({
    method: 'POST',
    url: `/call-center/openapi/v2/workOrderRecord/uip/accept`,
    data
  })
}
/** 查询所有派给B2C的工单 */
export function getForB2C() {
  return request({
    method: 'GET',
    url: '/call-center/openapi/v1/workOrderRecord/forB2C'
  })
}
/** 查询所有派给MSC的工单 */
export function getForMSC() {
  return request({
    method: 'GET',
    url: '/call-center/openapi/v1/workOrderRecord/forMSC'
  })
}
/** 查询所有派给QUP的工单 */
export function getForQUP() {
  return request({
    method: 'GET',
    url: '/call-center/openapi/v1/workOrderRecord/forQUP'
  })
}
/** 查询所有派给TOP的工单 */
export function getForTOP() {
  return request({
    method: 'GET',
    url: '/call-center/openapi/v1/workOrderRecord/forTOP'
  })
}

/**
 * 根据订单ID查询订单详细
 * @param {formData} data 查询条件
 */
export function getWorkOrderDetail(data) {
  return request({
    url: `/call-center/api/v1/workOrderRecord/page`,
    method: 'POST',
    data
  })
}
/**
 * 根据查询是否有未完结工单
 * @param {formData} data 查询条件
 */
export function getHaveWorkOrder(v) {
  const data = {
    filtersRaw: [
      filtersRaw('contactPhone', v, '='),
      filtersRaw('workOrderState', '[0,1,2]', 'in')
    ],
    onlyCountFlag: true,
    pageFlag: false
  }
  return request({
    url: `/call-center/api/v1/workOrderRecord/page`,
    method: 'POST',
    data: pageRequsetBody(data)
  })
}
/**
 * 根据工单编号查详情
 * @param {string} code 工单workOrderCode
 */
export function getWorkOrderByCode(workOrderCode) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/getByCode/${workOrderCode}`,
    method: 'GET'
  })
}

/**
 * 上报异常
 * @param {body} data data
 */
export function reportAbnormal(data) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/reportAbnormal`,
    method: 'PUT',
    data
  })
}

/**
 * 删除附件
 * @param {body} data 附件信息
 */
export function deleteAnnex(data) {
  return request({
    url: `/call-center/api/v1/workOrderRecord/annex`,
    method: 'delete',
    data
  })
}

/**
 * 查询产品信息 (TODO:暂时未使用，用于所有产品)
 * @param {String} productCode 产品编码
 */
export function getProductInfo(productCode) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/productName/${productCode}`,
    method: 'get'
  })
}
/**
 * 根据平台单号获取电商产品信息
 * @param {*} pCode
 * @returns
 */
export function productInfoByPCode(pCode) {
  return request({
    url: `/call-center/api/v1/mscInfo/productInfo/${pCode}`,
    method: 'GET'
  })
}
/**
 * 零售订单产品信息
 * @param {*} saleOrderCode
 * @returns
 */
export function retailDetail(saleOrderCode) {
  return request({
    url: `/call-center/api/v1/topInfo/retailDetail/v2/${saleOrderCode}`,
    method: 'POST'
  })
}
/**
 * 根据电商订单号获取平台单号
 * @param {*} ecCode
 * @returns
 */
export function getPlateCodeByEcCode(ecCode) {
  return request({
    url: `/call-center/api/v1/mscInfo/getPlateCode/${ecCode}`,
    method: 'GET'
  })
}
/**
 * 检查经销商是否在营业
 * @param {*} disCode
 * @returns
 */
export function checkDistributorByDisCode(disCode) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/queryCustom/${disCode}`,
    method: 'GET'
  }).then(res => {
    if (res.data && ([2, 5, '2', '5'].includes(res.data.businessStatus) || [0, '0'].includes(res.data.status))) {
      return Promise.reject(res)
    }
    return Promise.resolve(res)
  })
}
/**
 * 根据四级地址匹配经销商
 * @param {*} data
 * @returns
 */
export function getDealerMatch(data) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/matchCustom`,
    method: 'POST',
    data
  })
}
/**
 * 获取工单接收岗位
 * @param {*} workOrderId
 * @returns
 */
export function handlerPostList(workOrderId) {
  return request({
    url: `/call-center/api/v1/workOrderHandler/ressign/handlerPost/${workOrderId}`,
    method: 'POST'
  })
}
/**
 * 获取工单处理人列表
 * @param {*} workOrderId
 * @returns
 */
export function getHandlerList(workOrderId) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = [
    { id: 'workOrderId', property: 'workOrderId', value: workOrderId, operator: '=' }
  ]
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/call-center/api/v1/workOrderHandler/page',
    method: 'POST',
    data
  }).then(res => {
    return res.data.records
  })
}
/**
 * 改派处理人岗位
 * @param {*} workOrderId
 * @param {*} data
 * @returns
 */
export function handlerUpdate(workOrderId, data) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/reassign/handler/${workOrderId}`,
    method: 'PUT',
    data
  })
}
/**
 * 抄送查询接收人岗位
 * @param {*} workOrderId
 * @param {*} operationType
 * @returns
 */
export function queryReceiver(workOrderId, operationType = '02') {
  return request({
    url: `/call-center/api/v2/workOrderOperationReceiver/queryReceiver/${workOrderId}/${operationType}`,
    method: 'POST'
  }).then(res => {
    if (res.data['04']) {
      return res.data['04'] || []
    }
    return []
  })
}
/**
 * 抄送接收人岗位
 * @param {*} workOrderId
 * @param {*} data
 * @returns
 */
export function carbonCopyHandlerPost(workOrderId, data) {
  return request({
    url: `/call-center/api/v2/workOrderRecord/carbonCopy/handler/${workOrderId}`,
    method: 'POST',
    data
  })
}
/**
 * 自动匹配接收岗位
 * @param {*} data
 * @returns
 */
export function automaticHandlerPost(data) {
  return request({
    url: '/call-center/api/v2/workOrderRecord/automatic/handlerPost',
    method: 'PUT',
    data
  })
}

/**
 * 批量变更处理人:新增处理人
 * @param {*} data
 * @returns
 */
export function changeHandlerAdd(data) {
  return request({
    url: '/call-center/api/v1/workOrderHandler/batch/handler/add',
    method: 'POST',
    data
  })
}
/**
 * 批量变更处理人:删除处理人
 * @param {*} data
 * @returns
 */
export function changeHandlerDel(data) {
  return request({
    url: '/call-center/api/v1/workOrderHandler/batch/handler/delete',
    method: 'POST',
    data
  })
}
/**
 * 批量变更处理人:修改处理人
 * @param {*} data
 * @returns
 */
export function changeHandlerEdit(data) {
  return request({
    url: '/call-center/api/v1/workOrderHandler/batch/handler/edit',
    method: 'POST',
    data
  })
}
/**
 * 批量变更处理人:批量查询工单处理人
 * @param {*} data
 * @returns
 */
export function changeHandlerQuery(data) {
  return request({
    url: '/call-center/api/v1/workOrderHandler/batch/handler/queryHandler',
    method: 'POST',
    data
  })
}
/**
 * 更新工单商家回复状态
 * @param {*} id
 * @param {*} applyStatus
 * @returns
 */
export function invalidReturnApplyStatus(id, applyStatus) {
  const data = new FormData()
  data.append('applyStatus', applyStatus)
  return request({
    url: `/call-center/api/v2/workOrderRecord/apply/status/${id}`,
    method: 'PUT',
    data
  })
}
/**
 * 根据来源单号查询配送信息
 * @param {*} codes
 * @returns
 */
export function queryDeliveryBySources(codes) {
  return request({
    url: '/call-center/api/v2/workOrderRecord/queryDeliveryBySources',
    method: 'POST',
    data: codes
  })
}
